package org.idea.qiyu.threadpool.demo.controller;

import org.apache.catalina.User;
import org.idea.qiyu.threadpool.demo.asynchandle.AsyncHandlerData;
import org.idea.qiyu.threadpool.demo.asynchandle.AsyncHandlerService;
import org.idea.qiyu.threadpool.demo.exception.DivTask;
import org.idea.qiyu.threadpool.demo.sendemail.EmailDTO;
import org.idea.qiyu.threadpool.demo.sendemail.SendEmailService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Author linhao
 * @Date created in 11:27 上午 2021/10/16
 */
@RestController
@RequestMapping(value = "/test")
public class TestController {

    @Resource
    private SendEmailService sendEmailService;
    @Resource
    private AsyncHandlerService asyncMultiConsumerHandlerHandler;
    @Resource
    private org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor taskExecutor;

    @GetMapping(value = "/test-thread")
    public void testThread(){
        System.out.println("test");
        taskExecutor.execute(new Runnable() {
            @Override
            public void run() {
                User user = null;
                boolean flag = true && false || "test".equals(user.getFullName());
            }
        });
    }

    @GetMapping(value = "/send-async-data")
    public boolean sendAsyncData(){
        AsyncHandlerData asyncHandlerData = new AsyncHandlerData();
        asyncHandlerData.setDataInfo("data info");
//        boolean status = asyncSingleConsumerHandler.putTask(asyncHandlerData);
        boolean status = asyncMultiConsumerHandlerHandler.putTask(asyncHandlerData);
        if(!status){
            throw new RuntimeException("insert fail");
        }
        return status;
    }

    @GetMapping(value = "/send-email")
    public boolean sendEmail() {
        EmailDTO emailDTO = new EmailDTO();
        emailDTO.setContent("测试文案");
        emailDTO.setReceiver("idea");
        emailDTO.setTitle("邮件标题");
        sendEmailService.sendEmail(emailDTO);
        return true;
    }

}
